package com.yyx.beautifylib.ui.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.muzhi.camerasdk.library.views.HorizontalListView;
import com.yyx.beautifylib.R;
import com.yyx.beautifylib.model.BLPaint;
import com.yyx.beautifylib.model.BLMosaicParam;
import com.yyx.beautifylib.scrawl.DrawingBoardView;
import com.yyx.beautifylib.scrawl.ScrawlTools;
import com.yyx.beautifylib.ui.activity.base.BLToolBarActivity;
import com.yyx.beautifylib.utils.BLConfigManager;
import com.yyx.beautifylib.utils.BLSelectedStateListDrawable;
import com.yyx.beautifylib.view.holocolorpicker.ColorPicker;
import com.yyx.beautifylib.view.holocolorpicker.OpacityBar;
import com.yyx.beautifylib.view.holocolorpicker.SVBar;

import java.util.ArrayList;
import java.util.List;

import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodlePath;
import cn.hzw.doodle.DoodlePen;
import cn.hzw.doodle.DoodleShape;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleColor;
import cn.hzw.doodle.core.IDoodleItemListener;
import cn.hzw.doodle.core.IDoodlePen;
import cn.hzw.doodle.core.IDoodleSelectableItem;
import cn.hzw.doodle.core.IDoodleTouchDetector;

/**
 * @Desc:马赛克
 * @Author:  86133
 * @Time:  2020/4/11
 */
public class BLMosaicActivity extends BLToolBarActivity implements View.OnClickListener {
    private DrawingBoardView mDrawingView;

    private ScrawlTools mScrawlTools;
    private BLMosaicParam mParam;
    private Bitmap mSource;

    private FrameLayout mFrameLayout;
    private ImageView mosaic_one;
    private ImageView mosaic_two;
    private ImageView return_last;
    private IDoodle mDoodle;
    private DoodleView mDoodleView;
    private DoodleOnTouchGestureListener mTouchGestureListener;

    @Override
    protected int getContentLayoutId() {
        return R.layout.bl_activity_mosaic;
    }

    @Override
    protected void customToolBarStyle() {
        mToolbar.setTitle("马赛克");
        mToolbar.inflateMenu(R.menu.menu_preview);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (item.getItemId() == R.id.preview_menu) {
                    mDoodle.save();
                }
                return false;
            }
        });
    }

    @Override
    protected void initView() {
        mDrawingView = getViewById(R.id.scrawl_draw_view);

        mFrameLayout = getViewById(R.id.doodle_container);
        mosaic_one = getViewById(R.id.mosaic_one);
        mosaic_two = getViewById(R.id.mosaic_two);
        return_last = getViewById(R.id.return_last);

    }

    @Override
    protected void otherLogic() {
//        mPaintColor = BLConfigManager.getPrimaryColor();
        mParam = getIntent().getParcelableExtra(BLMosaicParam.KEY);
        mSource = BLMosaicParam.bitmap;
        mScrawlTools = new ScrawlTools(mInstance, mDrawingView, mSource);

        mDoodle = mDoodleView = new DoodleView(this, mSource, false, new IDoodleListener() {
            @Override
            public void onSaved(IDoodle doodle, Bitmap bitmap, Runnable callback) {
                mSource = bitmap;

                //涂鸦完成
                setResult(RESULT_OK);
                BLMosaicParam.bitmap = mSource;
                onBackPressed();
                finish();
            }

            public void onError(int i, String msg) {
            }

            @Override
            public void onReady(IDoodle doodle) {

                // 设置初始值
                mDoodle.setSize(150);//画笔粗细
                // 选择画笔
                mDoodle.setPen(DoodlePen.MOSAIC);
                mDoodle.setShape(DoodleShape.HAND_WRITE);
                mDoodle.setColor(DoodlePath.getMosaicColor(mDoodle, DoodlePath.MOSAIC_LEVEL_2));
            }
        }, null);
        mTouchGestureListener = new DoodleOnTouchGestureListener(mDoodleView, new DoodleOnTouchGestureListener.ISelectionListener() {
            // save states before being selected
            IDoodlePen mLastPen = null;
            IDoodleColor mLastColor = null;
            Float mSize = null;

            IDoodleItemListener mIDoodleItemListener = new IDoodleItemListener() {
                @Override
                public void onPropertyChanged(int property) {
                }
            };

            @Override
            public void onSelectedItem(IDoodle doodle, IDoodleSelectableItem selectableItem, boolean selected) {
            }

            @Override
            public void onCreateSelectableItem(IDoodle doodle, float x, float y) {
            }
        }) {
            @Override
            public void setSupportScaleItem(boolean supportScaleItem) {
                super.setSupportScaleItem(supportScaleItem);
            }
        };
        IDoodleTouchDetector detector = new DoodleTouchDetector(getApplicationContext(), mTouchGestureListener);
        mDoodleView.setDefaultTouchDetector(detector);

        mDoodle.setIsDrawableOutside(false);
        mFrameLayout.addView(mDoodleView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }


    @Override
    protected void setListener() {
        mosaic_one.setOnClickListener(this);
        mosaic_two.setOnClickListener(this);
        return_last.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        int resId = v.getId();
        if (resId == R.id.mosaic_one) {
            mosaic_one.setImageDrawable(getResources().getDrawable(R.drawable.mosaic_one));
            mosaic_two.setImageDrawable(getResources().getDrawable(R.drawable.mosaic_two_un));
            mDoodle.setColor(DoodlePath.getMosaicColor(mDoodle, DoodlePath.MOSAIC_LEVEL_2));
        } else if (resId == R.id.mosaic_two) {
            mosaic_one.setImageDrawable(getResources().getDrawable(R.drawable.mosaic_one_un));
            mosaic_two.setImageDrawable(getResources().getDrawable(R.drawable.mosaic_two));
            mDoodle.setColor(DoodlePath.getMosaicColor(mDoodle, DoodlePath.MOSAIC_LEVEL_1));
        } else if (resId == R.id.return_last) {
            mDoodle.undo();
        }
    }
}
